from src.Car import Car
from src.Truck import Truck


class CarsShop:

    def __init__(self, io_strategy, db_strategy):
        self.catalog = list()
        self.io_strategy = io_strategy
        self.db_strategy = db_strategy
        self.path = "aam2307/st13/shop.data"

    def add_car(self):
        c = Car(self.io_strategy, len(self.catalog) + 1)
        self.catalog.append(c)
        self.io_strategy.saved()

    def add_truck(self):
        t = Truck(self.io_strategy, len(self.catalog) + 1)
        self.catalog.append(t)
        self.io_strategy.saved()

    def show_catalog(self):
        return self.catalog

    def save(self):
        self.db_strategy.save(self.path, self.catalog)

    def load(self):
        self.catalog = self.db_strategy.load(self.path)

    def clear_all(self):
        try:
            self.catalog.clear()
            self.io_strategy.output("Очищено")
            self.io_strategy.print_line()
        except Exception as e:
            print(e.args[0])

    def edit_car(self, num):
        for i in self.catalog:
            if i.inventory_number == num:
                i.edit()
                break

    def get_by_number(self, inv_choice):
        for i in self.catalog:
            if i.inventory_number == inv_choice:
                return i
